<?php

namespace teamug;

/**
 * Quick and easy way to get data from $_GET, $_POST, $_COOKIE and handle missing keys
 * Easier to push values into double quotes too
 */

class RequestData {
  
  const GET = 1;
  const POST = 2;
  const COOKIE = 3;
  
  private $source;
  
  
  /**
   * Construct, and choose data source
   * 
   * @param integer $source Use data source RequestData::GET or RequestData::POST or RequestData::COOOKIE
   */
  public function __construct($source=2) {

    switch($source) {
      case 1:
        $this->source = &$_GET;
        return;
      case 2:
        $this->source = &$_POST;
        return;
      case 3:
        $this->source = &$_COOKIE;
        return;
    }
  }
  
  
  /**
   * Get value from $_GET, $_POST or $_COOKIE
   * 
   * @param string $name Key name
   * @return mixed/null Value if found, or null if not
   */
  public function __get($name) {
    
    if (array_key_exists($name, $this->source)) {
      return $this->source[$name];
    } else {
      return null;
    }
  }
  
  public function getArrayFromCsv($csv) {
    
    $csv = preg_replace('[\s]', '', $csv);
    $keys = explode(',', $csv);
    
    $retval = array();
    
    foreach($keys AS $key) {
      $retval[$key] = $this->__get($key);
    }
      
    return $retval;
  }
  
  
  
  public function getArrayExcludingCsv($csv) {
    
    $csv = preg_replace('[\s]', '', $csv);
    $unwanted_keys = explode(',', $csv);
    
    $retval = array();

    $source_keys = array_keys($this->source);
    
    foreach($source_keys AS $key) {
      
      if (!\in_array($key, $unwanted_keys)) {
        $retval[$key] = $this->__get($key);
      }
    }
    return $retval;
  }
  
  
}

